<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

//连接
$host = '127.0.0.1';//ip
$port = '5672';//端口
$user = 'guest'; //用户
$password = 'guest'; //密码
$vhost = '/'; //空间
$connection = new AMQPConnection($host, $port,$user, $password,$vhost);
$channel = $connection->channel();

 /*
     name: 交换机名字
     type: 交换机类型
     passive: false
     durable: true // 交换机将在服务器重启后生存。
     auto_delete: false //通道关闭的时候，交换机不会被删除
 */
 $channel->exchange_declare('exchange_fanout_clevercode', 'fanout', false, true, false);


for($i = 1; $i <= 300;$i++)
{
    //sleep(1);
    /**
     * Publishes a message
     *
     * @param AMQPMessage $msg
     * @param string $exchange
     * @param string $routing_key
     * @param bool $mandatory
     * @param bool $immediate
     * @param null $ticket
     */
    $data = "msg info:{$i}";
    $msg = new AMQPMessage($data);
    $channel->basic_publish($msg, 'exchange_fanout_clevercode');

    echo "send :{$data}\n";
}

$channel->close();
$connection->close();